/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.enac.geoapp.parser.pln;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;


/**
 * Sax parser for xml files containing flight plan created by airplane 3
 * @author Mathieu Magnaudet
 *
 */
public class FlightPlanSaxParser  {
	private List<MapPoint> mainFlightPlan;
	private List<MapPoint> alternateFlightPlan;
	private SimpleContentHandler handler;
	private static Logger logger = Logger.getLogger(FlightPlanSaxParser.class);
	
	/**
	 * 
	 * @return the alternate flight plan
	 */
	public List<MapPoint> getAlternateFlightPlan() {

		return alternateFlightPlan;
	}

	/**
	 * 
	 * @return the primary flight plan
	 */
	public List<MapPoint> getPrimaryFlightPlan() {
		return mainFlightPlan;
	}

	/**
	 * Parses the file
	 * @param path the file to parse
	 */
	public void parse(String path) {
		XMLReader saxReader;
		handler = new SimpleContentHandler();
		try {
			saxReader = XMLReaderFactory
					.createXMLReader("org.apache.xerces.parsers.SAXParser");
			saxReader.setContentHandler(handler);
			saxReader.parse(path);
		} catch (SAXException e) {
			logger.fatal(e.getMessage());
			System.exit(0);
		} catch (IOException e) {
			logger.fatal(e.getMessage());
			System.exit(0);
		}
		
		mainFlightPlan = handler.getMainPlan();
		alternateFlightPlan = handler.getAltPlan();
	}

}
